const MAX_LIMIT = 100
Page({

  /** 
   * 页面的初始数据 
   */
  data: {
    commentList: [],
    tupianList: [],
    shipinList: [],
    winWidth: 0,
    winHeight: 0,
    currentTab: 0,
  },

  //  tab切换逻辑
  swichNav: function (e) {

    var that = this;

    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  remove: function (e) {
    let id = e.currentTarget.dataset.id
    console.log(id)
    const db = wx.cloud.database();
    db.collection("shouchang").doc(id).remove({
      success: res => {
        wx.showToast({
          title: '删除成功',
        })
        var tupianList=this.data.tupianList;
        tupianList.splice(e.target.dataset.index,1);
        this.setData({
          tupianList:tupianList
        })
      }, fail: err => {
        wx.showToast({
          title: '删除失败',
        })
      }
    })

  },
  remove1: function (e) {
    let id = e.currentTarget.dataset.id
    console.log(id)
    const db = wx.cloud.database();
    db.collection("shouchang").doc(id).remove({
      success: res => {
        wx.showToast({
          title: '删除成功',
        })
        var commentList=this.data.commentList;
        commentList.splice(e.target.dataset.index,1);
        this.setData({
          commentList:commentList
        })
 //删除成功重新加载
      }, fail: err => {
        wx.showToast({
          title: '删除失败',
        })
      }
    })

  },
  remove2: function (e) {
    let id = e.currentTarget.dataset.id
    console.log(id)
    const db = wx.cloud.database();
    db.collection("shouchang").doc(id).remove({
      success: res => {
        wx.showToast({
          title: '删除成功',
        })
        var shipinList=this.data.shipinList;
        shipinList.splice(e.target.dataset.index,1);
        this.setData({
          shipinList:shipinList
        })
 //删除成功重新加载
      }, fail: err => {
        wx.showToast({
          title: '删除失败',
        })
      }
    })

  },

  bindChange: function (e) {

    var that = this;
    that.setData({ currentTab: e.detail.current });

  },

  /** 
   * 生命周期函数--监听页面加载 
   */
  onLoad: function (options) {
    var that = this;

    /**
     * 获取当前设备的宽高
     */
    wx.getSystemInfo({

      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }

    });


    this._getListByCloudfu()
    this._getListByCloudfu1()
    this._getListByCloudfu2()

  },

  // 漫贴
  _getListByCloudfu() {
    wx.showLoading({
      title: '加载中',
    })
    wx.cloud.callFunction({
      name: 'shouchang',
      data: {
        $url: 'getListByOpenid2',
        start: this.data.commentList.length,
        count: MAX_LIMIT
      }

    }).then((res) => {
      console.log(res)
      this.setData({
        commentList: res.result
      })
      wx.hideLoading()
    })
  },
  goComment(event) {

    wx.navigateTo({
      url: '/pages/index/mantie/mantie-nr/mantie-nr?tieziId=' + event.target.dataset.tieziid,
    })
  },

  // 视频
  _getListByCloudfu2() {
    wx.showLoading({
      title: '加载中',
    })
    wx.cloud.callFunction({
      name: 'shouchang',
      data: {
        $url: 'getListByOpenid3',
        start: this.data.commentList.length,
        count: MAX_LIMIT
      }

    }).then((res) => {
      console.log(res)
      this.setData({
        shipinList: res.result
      })
      wx.hideLoading()
    })
  },
  goComment2(event) {

    wx.navigateTo({
      url: '/pages/index/shipin/shipin-nr/shipin-nr?shipinId=' + event.target.dataset.shipinid,
    })
  },

  // 图片

  _getListByCloudfu1() {
    wx.showLoading({
      title: '加载中',
    })
    wx.cloud.callFunction({
      name: 'shouchang',
      data: {
        $url: 'getListByOpenid',
        start: this.data.commentList.length,
        count: MAX_LIMIT
      }

    }).then((res) => {
      console.log(res)
      this.setData({
        tupianList: res.result
      })
      wx.hideLoading()
    })
  },
  goComment1(event) {

    wx.navigateTo({
      url: '/pages/index/meitu/meitu-nr/meitu-nr?tupianId=' + event.target.dataset.tupianid,
    })
  },

  /** 
   * 生命周期函数--监听页面初次渲染完成 
   */
  onReady: function () {

  },

  /** 
   * 生命周期函数--监听页面显示 
   */
  onShow: function () {

  },

  /** 
   * 生命周期函数--监听页面隐藏 
   */
  onHide: function () {

  },

  /** 
   * 生命周期函数--监听页面卸载 
   */
  onUnload: function () {

  },

  /** 
   * 页面相关事件处理函数--监听用户下拉动作 
   */
  onPullDownRefresh: function () {

  },

  /** 
   * 页面上拉触底事件的处理函数 
   */
  onReachBottom: function () {

  },

  /** 
   * 用户点击右上角分享 
   */
  onShareAppMessage: function () {

  }
})
